﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using ApiService.EF;
using ApiService.EF.Entities;
using ApiService.Model;
using ApiService.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using MySql.Data.MySqlClient;

namespace ApiService.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly IConfiguration _config;
        private readonly IOptionsSnapshot<DemoConfig> _configDemo;
        private readonly IUserService _userService;
        private IServiceProvider _serviceProvider;

        public ValuesController(IConfiguration config,
            IOptionsSnapshot<DemoConfig> configDemo,
            IUserService userService,
            IServiceProvider serviceProvider
            )
        {
            _config = config;
            _configDemo = configDemo;
            _userService = userService;
            _serviceProvider = serviceProvider;
        }

        [HttpGet]
        public string Get()
        {

            return "222222";
            //两种方式获取配置文件的数据
            //var demo = new DemoConfig
            //{
            //    Name = _config["name"],
            //    Age = int.Parse(_config["age"]),
            //    Env = _config["env"]
            //};
            //var demo = _configDemo.Value;
            //return demo;
          

            return _config["connectionStrings:default"];
        }

        [HttpGet]
        public string Add()
        {
            var a = _serviceProvider.GetService(typeof(ApplicationDbContext));
            Debug.WriteLine(a.Equals(_userService.dbcontext));
            return _userService.Add();
        }

        //[HttpGet]
        //public string Edit()
        //{

        //    var entity = _dbContext.Users.FirstOrDefault(p => p.Id == 1);

        //    entity.RealName = "张三丰";

        //    int result = _dbContext.SaveChanges();
        //    return result.ToString();
        //}

        //[HttpGet]
        //public List<UserEntity> Select(int pageIndex)
        //{
        //    int pageSize = 10;
        //    var entitys = _dbContext.Users
        //        .Skip((pageIndex - 1) * pageSize)
        //        .Take(pageSize)
        //        .ToList();
        //    return entitys;
        //}

        //[HttpGet]
        //public int Delete()
        //{
        //    int i = 0;
        //    var entity = _dbContext.Users.FirstOrDefault(p => p.Id < 11);
        //    if (null != entity)
        //    {
        //        _dbContext.Users.Remove(entity);
        //        i = _dbContext.SaveChanges();
        //    }
        //    return i;
        //}
    }
}
